package com.sanpu.kxe.business.main.presenter;

import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.NewsContentOutput;
import com.sanpu.kxe.business.main.contract.MainConfig;
import com.sanpu.kxe.business.main.contract.NewsColumnContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：新闻列表页
 * Created by 薛勇军 on 2018/8/29.
 */

public class NewsColumnPresenter implements NewsColumnContract.Presenter {

    private NewsColumnContract.View mView;

    private CompositeSubscription mSubscription;

    public NewsColumnPresenter(NewsColumnContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void getNewsList(final int nowPage, String columnType) {
        Subscription subscription = MainModel.getInstance().getNewsList(nowPage, columnType)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<NewsContentOutput>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showLoadMore(CommonConfig.LOADMOREFAIL);
                    }

                    @Override
                    public void onNext(List<NewsContentOutput> newsContentOutputs) {
                        if (newsContentOutputs != null) {
                            for (int i=0;i<newsContentOutputs.size();i++) {
                                String picturePath = newsContentOutputs.get(i).getPicturePath();
                                if (TextUtils.isEmpty(picturePath)) {
                                    newsContentOutputs.get(i).setItemType(MainConfig.NEWS_ZERO_IAMGE);
                                } else {
                                    String[] pictures = picturePath.split(",");
                                    if (pictures.length>1) {
                                        newsContentOutputs.get(i).setItemType(MainConfig.NEWS_THREE_IMAGE);
                                    } else {
                                        newsContentOutputs.get(i).setItemType(MainConfig.NEWS_ONE_IMAGE);
                                    }
                                }
                            }
                            mView.showNewsListView(newsContentOutputs, nowPage == 0);
                            if (newsContentOutputs.size() < CommonConfig.PAGENUMER) {
                                mView.showLoadMore(CommonConfig.LOADMOREEND);
                            } else {
                                mView.showLoadMore(CommonConfig.LOADMORECOMPLETE);
                            }

                        } else {
                            mView.showLoadMore(CommonConfig.LOADMOREEND);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void OnItemClick(NewsContentOutput contentOutput) {
        if (contentOutput != null && !TextUtils.isEmpty(contentOutput.getLinkAddress())) {
            statisticalClick(contentOutput.getNewsId());
            String picturePath = contentOutput.getPicturePath();
            if (!TextUtils.isEmpty(picturePath)) {
                String[] pictures = picturePath.split(",");
                if (pictures.length>0) {
                    picturePath = pictures[0];
                }
            }
//            MainOpenProvider.getInstance().openNewsContentActivity(mView.getContext(), contentOutput.getLinkAddress(),
//                    contentOutput.getNewsTitle(), contentOutput.getNewsContent(), picturePath);
        }
    }

    /**
     * 记录点击
     */
    private void statisticalClick(String newsId) {
        Subscription subscription = MainModel.getInstance().statisticalClick(newsId)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe();
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
